﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DATN
{
    public partial class InternalSite : System.Web.UI.MasterPage
    {
        SqlCommand cmd;
        SqlConnection cnn;
        int us_id;

        Thread thread;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] != null && Session["Us_Id"] != null)
                {
                    Lbt_UserName.Text = Session["UserName"].ToString();

                    NoteRemind();
                    
                }
            }
        }

        //Hàm khởi tạo thread:
        public void NoteRemind()
        {
            Lb_Remind.Text = "";
            thread = new Thread(new ThreadStart(WorkThreadFunction));
            thread.Start();
        }

        //Hàm chạy thread
        public void WorkThreadFunction()
        {
            try
            {
                while (true)
                {
                    NoteInfo();
                    Thread.Sleep(3000000);
                }
            }
            catch (Exception ex)
            {
                Lb_Remind.Text = "Lỗi";
            }
        }

        //Hàm lấy nội dung trong Note ra:
        protected void NoteInfo()
        {
             try{
                if(Session["Us_Id"] != null)
                {
                    us_id = Convert.ToInt16(Session["Us_Id"].ToString());
                    check_Note(us_id); 
                }else{
                    
                }
            }catch(Exception ex)
            {
                Response.Write("Lỗi");
            }
        }

        string Rmail = "";
        //Lấy top 5 note sắp tới. so với thời gian hiện tại --- > hiển thỉ:
        protected string check_Note(int us_id)
        {
            SqlDataReader rdr = null;
            try
            {
                string cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ConnectionString;
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                String queryString = "SELECT TOP 5 * FROM Notes WHERE Us_Id = @Us_Id"
                                            +" AND TimeRemind > @TimeRemind ";
                cmd = new SqlCommand(queryString, cnn);
                cmd.Parameters.Add("@Us_Id", SqlDbType.Int);
                cmd.Parameters["@Us_Id"].Value = us_id;

                cmd.Parameters.Add("@TimeRemind", SqlDbType.DateTime);
                cmd.Parameters["@TimeRemind"].Value = DateTime.Now.ToShortDateString();
                
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    // get the results of each column
                    //Rmail += (string)rdr["Name"] + "<br/>";
                    Rmail += "<script>"
                        + "$.jGrowl('" + (string)rdr["Name"] + "');"
                        + "</script>";
                   
                }

                Lb_Remind.Text = Rmail;
            }
            catch (Exception ex)
            {
                Response.Write(" Lỗi ở Check Note: " + ex);
            }
            return Rmail;
        }

        protected void Lbt_Logout_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            Response.Redirect("../Account/Login.aspx");
        }

    }
}
